﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TRPOP.CMS.RegAssembly;
using System.Text.RegularExpressions;
using TRPOP.Common;
using System.Collections;
/*
 * 
 *   基本信息的处理
 * 
 */
namespace TRPOP.CMS.Template.GenerateCS
{
    /// <summary>
    /// 将cms标签替换为csharp语言
    /// </summary>
    public partial class ConvertTags
    {
        /*
         * 
         *  解析if条件语句  
         *   模式： 一行一行的解析
            <if   test="$a > $b"  >         @if
            <elseif test="$a < $b" >        #elseif
            <else>                          $else
            </if>                           %/if
         ====>
            if($a > $b)                     @if
            {                               @if
            .........
            }                               #elseif
            else if($a < $b)                #elseif
            {                               #elseif
            .....
            }                               $else
            else                            $else
            {                               $else
              ............
            }                               %/if
         * 
         */

        /// <summary>
        /// 解析if系列的标签
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="oMatch"></param>
        /// <param name="strSpaces"></param>
        /// <param name="intBraceCount"></param>
        /// <returns></returns>
        public static string ConvertTags_If(ref string strSource, Match oMatch, ref string strSpaces)
        {
            string strReturn = string.Empty;

            //将标签前面的html字符串输出
            if (oMatch.Index > 0)
            {
                strReturn += strSpaces + string.Format("oTemplatePageInfo.strBuildHtml.Append    (\"{0}\");{1}", CommonMethord.ConvertSpecialChar(strSource.Substring(0, oMatch.Index)), Environment.NewLine);
            }

            //分析if , 如果是 </if>
            if (oMatch.Value.Trim().ToLower() == @"</if>")
            {
                //如果是 </if> 则输出 } 
                strSpaces = strSpaces.RTrim("\t");
                strReturn += strSpaces + "}" + Environment.NewLine;
                strSource = strSource.Replace(oMatch.Value, string.Empty);//去除标签
            }
            else if (oMatch.Value.Trim().ToLower() == @"<else>")
            {
                //如果是 <else> 则输出  }else{
                strSpaces = strSpaces.RTrim("\t");
                strReturn += strSpaces + "}" + Environment.NewLine;
                strReturn += strSpaces + "else" + Environment.NewLine;
                strReturn += strSpaces + "{" + Environment.NewLine;
                strSpaces = strSpaces + "\t";
                strSource = strSource.Replace(oMatch.Value, string.Empty);//去除标签
            }
            else if ((oMatch.Groups["TagName"].Value.Trim().ToLower() == @"if" || oMatch.Groups["TagName"].Value.Trim().ToLower() == @"elseif"))
            {
                //Csharp对应的语法
                string strCSharpGrammar = "if";
                if (oMatch.Groups["TagName"].Value.Trim().ToLower() == @"elseif")
                {
                    strCSharpGrammar = "else if";
                }

                //找到 <if test="..." > 或 <elseif test="..." >
                Match oIfMatch = oTags_IfGramar.Match(strSource, oMatch.Index);
                if (oIfMatch.Success && oIfMatch.Groups["test"].Value.IsEmpty() == false)
                {
                    string strTest = ConvertTags_If_test(oIfMatch.Groups["test"].Value);
                    if (strCSharpGrammar == "else if")
                    {
                        strSpaces = strSpaces.RTrim("\t");
                        strReturn += strSpaces + "}" + Environment.NewLine;
                    }
                    strReturn += strSpaces + strCSharpGrammar + "( " + strTest + " )" + Environment.NewLine;
                    strReturn += strSpaces + "{" + Environment.NewLine;
                    strSpaces = strSpaces + "\t";
                    strSource = strSource.Replace(oIfMatch.Value, string.Empty);
                }
                else
                {
                    //语法错误
                    strSource = strSource.Replace(oMatch.Value, TagsError.GetTagsError(TagsError.TagsErrorAnalysis.ArrtibuteError));
                }
            }

            //去除标签语法
            if (!strSource.IsEmpty() && strSource.Length > oMatch.Index + 1)
            {
                strSource = strSource.Substring(oMatch.Index);
            }

            return strReturn;
        }
        /// <summary>
        /// 将 test="..." 里的 ... 转换为csharp
        /// </summary>
        /// <param name="strSource"></param>
        /// <returns></returns>
        public static string ConvertTags_If_test(string strSource)
        {
            string strTestReturn = "( ";

            strSource = strSource.Replace(" and ", " && ").Replace(" or ", " || ");
            strTestReturn += ConvertStringToCsharpForTest(strSource).Replace("''", "0").Replace("'", "\"");

            strTestReturn += " )";
            return strTestReturn;
        }

        /// <summary>
        /// 将含有变量的字符串替换为chsarp中间代码 $a > $b
        /// </summary>
        /// <param name="strSource"></param>
        /// <returns></returns>
        public static string ConvertStringToCsharpForTest(string strSource)
        {
            //如果没有可解析的变量，则直接返回
            if (strSource.Contains("$") == false)
                return strSource;

            string strReturn = strSource;

            //如果不包含 字符加运算，则均转换为int
            bool IsParse = true;
            if (strReturn.Contains(" . ") || strReturn.Contains("empty("))
            {
                IsParse = false;
            }

            strReturn = strReturn.Replace(" . ", " + ");

            //找到每个变量
            Dictionary<string, string> oDicList = new Dictionary<string, string>();
            foreach (Match oTempMatch in oTags_AllTypeVarWithNoSign.Matches(strReturn))
            {
                if (!oDicList.ContainsKey(oTempMatch.Value))
                {
                    oDicList.Add(oTempMatch.Value,
                        string.Format("GetTagValue(\"{0}\", \"{1}\", \"{2}\"){3}", oTempMatch.Groups["DataSet"].Value.Trim(), oTempMatch.Groups["index"].Value.Trim(), oTempMatch.Groups["var"].Value.Trim(), (IsParse ? ".ToInt()" : ""))
                                );
                }
            }

            //将每个变量替换为解析函数，将其他的字符串替换为Csharp定义的字符串
            foreach (KeyValuePair<string, string> item in oDicList)
            {
                strReturn = strReturn.Replace(item.Key, item.Value);
            }
            return strReturn;
        }
    }
}